<?PHP
/******************************
* A common set of functions
* to be used on all pages
* include libraries and alike
******************************/
/************************************************
Remove the magic quote madness
************************************************/
if ( function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() ) {
	function stripslashes_deep($value) {
	    $value = is_array($value) ? array_map('stripslashes_deep', $value) : (isset($value) ? stripslashes($value) : null);
	    return $value;
	}

	$_POST = stripslashes_deep($_POST);
	$_GET = stripslashes_deep($_GET);
	$_COOKIE = stripslashes_deep($_COOKIE);
	$_REQUEST = stripslashes_deep($_REQUEST);
}

/************************************************
Define some globals
************************************************/
define("POMBY_DIR", realpath(dirname(__FILE__). '/../') );
date_default_timezone_set('GMT');

/************************************************
Error Handling
************************************************/
require_once(POMBY_DIR . '/classes/class.mvError.php');
error_reporting(E_ALL); // TODO: Handle for production env
$GLOBALS['mvError'] = array();
$GLOBALS['mvError']['debug'] = true;
set_error_handler(array('mvError', 'error'));

/************************************************
Config File and Settings
************************************************/
require_once(POMBY_DIR . '/classes/class.mvSettings.php');
if(is_file(POMBY_DIR . '/config/config.php')){
	include(POMBY_DIR . '/config/config.php');
}else{
	include(POMBY_DIR . '/config/default.php');
}

/************************************************
SQL Connection
************************************************/
if(!defined('DB_DNC')){
	try{
		require_once(POMBY_DIR . '/classes/class.mvPDO.php');
		require_once(POMBY_DIR . '/classes/class.mvPDOStatement.php');
		$GLOBALS['db'] = new mvPDO('mysql:host='.mvSettings::get('db_host').';dbname='.mvSettings::get('db_name'), mvSettings::get('db_user'), mvSettings::get('db_password')
			, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)
		);
		$GLOBALS['db']->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
		$GLOBALS['db']->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mvPDOStatement', array()));
	}catch(Exception $e){
		die('Could not connect to database, please contact technical support.  <br/><pre>' . print_r($e, true) . '</pre>');
	}
}

/************************************************
File Inclusions
************************************************/
// Required Classes
require_once(POMBY_DIR . '/inc/smarty/libs/Smarty.class.php');
require_once(POMBY_DIR . '/classes/class.mvTemplate.php');

function __autoload($className){
	require_once(POMBY_DIR . '/classes/class.'. $className.'.php');
}
if(function_exists('spl_autoload_register'))
	spl_autoload_register('__autoload');

// Session Functions
session_name("pomby");
if(!session_id())
	session_start();

?>